/*
* coder: Dzlua
* email: 505544956@qq.com
* time : 2018-04-16 17:42
*/

#include "ui/ui.hpp"

#include <iostream>

class MyCls
{
public:
    MyCls(int i) : m_n(i) {}
    static auto instance(int i) {
        return std::make_shared<MyCls>(i);
    }
public:
    int get() { return m_n; }
protected:
    int m_n;
};

template <typename T>
void show(ui::CMap<T, std::shared_ptr<MyCls>> &map) {
    for (auto itor = map.begin(); itor != map.end(); ++itor) {
        std::cout << itor->first << ":" << itor->second->get() << std::endl;
    }
}

template <typename T>
void rshow(ui::CMap<T, std::shared_ptr<MyCls>> &map) {
    for (auto itor = map.rbegin(); itor != map.rend(); ++itor) {
        std::cout << itor->first << ":" << itor->second->get() << std::endl;
    }
}

void test_map() {
    ui::CMap<int, std::shared_ptr<MyCls>> map;
    for (int i = 0; i <= 10; ++i) {
        map.insert(i, MyCls::instance(i));
    }
    show(map);
    auto ele = map.at(2);
    std::cout << "2:" << ele->get() << std::endl;
    //
    auto itor = map.find(3);
    if (itor != map.end())
        std::cout << itor->first << ":" << itor->second->get() << std::endl;
    //
    itor = map.find(ele);
    if (itor != map.end())
        std::cout << itor->first << ":" << itor->second->get() << std::endl;
    //
    std::cout << "-----erase 1------" << std::endl;
    map.erase(ele);
    show(map);
    std::cout << "------erase 2-----" << std::endl;
    map.erase(1);
    show(map);
    std::cout << "-----erase 3------" << std::endl;
    map.erase(map.find(0));
    show(map);
    std::cout << "-----------" << std::endl;
    std::cout << map.size() << "|" << map.empty() << std::endl;
    map.clear();
    std::cout << map.size() << "|" << map.empty() << std::endl;
}

void test_str_map() {
    ui::CMap<ui::string, std::shared_ptr<MyCls>> map;
    for (int i = 0; i <= 10; ++i) {
        map.insert(ui::any2str<int>(i), MyCls::instance(i));
    }
    show(map);
    auto ele = map.at("2");
    std::cout << "2:" << ele->get() << std::endl;
    //
    auto itor = map.find("3");
    if (itor != map.end())
        std::cout << itor->first << ":" << itor->second->get() << std::endl;
    //
    itor = map.find(ele);
    if (itor != map.end())
        std::cout << itor->first << ":" << itor->second->get() << std::endl;
    //
    std::cout << "-----erase 1------" << std::endl;
    map.erase(ele);
    show(map);
    std::cout << "------erase 2-----" << std::endl;
    map.erase("1");
    show(map);
    std::cout << "-----erase 3------" << std::endl;
    map.erase(map.find("0"));
    show(map);
    std::cout << "-----------" << std::endl;
    std::cout << map.size() << "|" << map.empty() << std::endl;
    map.clear();
    std::cout << map.size() << "|" << map.empty() << std::endl;
    
}

int main(int argc, char** argv)
{
    test_map();
    std::cout << "-----begin test_str_map------" << std::endl;
    test_str_map();
    return 0;
}